#region Copyright (C) 2007 Jens Winter

//Copyright (C) 2007 Jens Winter

//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.

//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace Flynn.Core
{
    public class ProjectLoader : IProjectLoader
    {
        public Project LoadProject(string projectFile)
        {
            try
            {
                XDocument projectDoc = XDocument.Load(projectFile);

                return LoadProject(projectDoc);
            }
            catch (Exception e)
            {
                throw new LoadingProjectException(e.Message);
            }
        }

        public Project LoadProject(XDocument projectDoc)
        {
            if (projectDoc == null)
                throw new ArgumentNullException("projectDoc");

            Project project = new Project();

            try
            {
                project.Name = (string) projectDoc.Root.Attribute("Name");
                project.Database = (string) projectDoc.Root.Element("Database");
                project.Versioning = (string) projectDoc.Root.Element("Versioning");

                IEnumerable<XElement> updateElements = projectDoc.Root.Elements("Update");
                foreach (XElement updateElement in updateElements)
                {
                    Update update = new Update(updateElement.Attribute("Version").Value);
                    update.Author = (string) updateElement.Attribute("Author");

                    if (updateElement.Attribute("CreatedDate") != null)
                        update.CreatedDate = (DateTime) updateElement.Attribute("CreatedDate");

                    update.Description = (string) updateElement.Element("Description");

                    IEnumerable<XElement> sqlStatementElements = updateElement.Elements("SqlStatement");
                    foreach (XElement sqlStatement in sqlStatementElements)
                    {
                        update.SqlStatements.Add((string) sqlStatement);
                    }

                    project.AddUpdate(update);
                }
            }
            catch (Exception e)
            {
                throw new LoadingProjectException(e.Message);
            }

            return project;
        }
    }

    public class LoadingProjectException : Exception
    {
        public LoadingProjectException(string message) : base(message)
        {
        }
    }
}